Skip to content

fix(github): read watched PRs without organization scopes - #40740

Open
Sidreyas wants to merge 1 commit into
pingdotgg:mainfrom
Sidreyas:sidreyas/fix-github-pr-watch-team-scopes-v2
Open

Sidreyas wants to merge 1 commit into
pingdotgg:mainfrom
Sidreyas:sidreyas/fix-github-pr-watch-team-scopes-v2

Conversation

@Sidreyas

Copy link
Copy Markdown
Contributor

Supersedes #32366. I closed that PR only to stay within the 3-open-PR limit. This is the same fix, ported fresh onto current main, not a merge of the old branch. Thanks to @sebassdc for confirming the failure on a real install (watches with reads: 0 and a null head while the scope errors kept coming).

Problem

A GitHub token with only repo, gist and workflow (no read:org) can't read a pull request's detail. PullRequestCore selects ... on Team { slug combinedSlug name avatarUrl } under reviewRequests. Every one of those fields needs read:org or read:discussion. GitHub checks scopes against the query text, so it refuses the whole query even when no team is requested, and even with reviewRequests(first: 0). The existing unreadable-reviewer fallback (#19549 / #19564) retries with first: 0, so it can't recover. A PR watch on such a token never sees checks or the review decision.

The review-thread read (ReviewThreads) fails the same way. Its Team fragment needs read:org, and so does ... on Organization { login } in the shared Reactions fragment.

Change

  • PullRequestCore and ReviewThreads take a $teamDetails: Boolean! and select Team fields under @include(if: $teamDetails). With the flag off, GitHub accepts the query (checked live).
  • readRequestedReviewers reads with teamDetails: true. If GitHub answers only with INSUFFICIENT_SCOPES, it retries once with teamDetails: false. A successful fallback is cached for 10 minutes per credential and host, so later detail and review-thread reads skip straight to it. A comment says why the key is per credential and host, and that a token that gains read:org reads teams again once the entry expires. This answers the CodeRabbit note on fix(github): read watched PRs without organization scopes #32366. The existing per-PR requested-reviewer refusal fallback still applies on top.
  • hasTeamReviewRequest also counts a request whose __typename is Team, so a team request still shows when its details weren't read. Individual reviewers, checks and the review decision are unchanged. Only team names and avatars are missing, and only for a credential that can't read them.
  • Reactions no longer selects ... on Organization { login }. An organization reactor still counts in totalCount, which the mapping already uses. The fragment is shared, so the regenerated issues.graphql.ts changes too: issue detail and comments stop selecting that field.
  • The generated *.graphql.ts files are regenerated with the package's graphqlgen.
  • A failed retry, or any other error, still fails as before.

Left alone on purpose: the PR listing (PullRequestRow's ... on Team { slug }) and the reviewer picker (ReviewerCandidates) also refuse this token (checked live). The listing needs the flag added to two queries and their callers. The reviewer picker is next to the open #35126. Both are separate follow-ups and not part of the watch-read fix.

Scope

This fixes the bug #32366 fixed, with the same approach, which Macroscope approved and CodeRabbit reviewed there. It's a small fix for an obvious bug: GitHub refuses a read for fields the token can't select, and nothing reaches the user except a watch that never fires. @sebassdc's report on #32366 shows the same failure on T3 Code 0.1.7. The unresolved review item from #32366 is addressed: the refusal assertion in the test checks the exact error and both retry calls. No contract, client or UI change.

Verification

End-to-end against real GitHub (read-only). I ran the real GitHubPullRequestApi read path, composed the way driver.ts builds it (GitHubPullRequestApi.layer ← GitHubAppAccess ← GitHubRest.layerWithDependencies, with the real credential, quota, request policy and fetch HTTP client). The credential came from GH_TOKEN, the same way the server reads it, and has scopes gist, repo, workflow only. The script calls getPullRequestDetail, getPullRequestActivity and listReviewThreadComments on three public PRs. The token was never printed, and the logs are redacted.

Before, with packages/source-control-github/src/server at origin/main 11224f6:

target: pingdotgg/t3code#40694 on github.com
getPullRequestDetail FAILED: {"_tag":"GitHubApiResponseError","status":200,"githubErrorTypes":["INSUFFICIENT_SCOPES",...],"refusedFields":["slug needs ['read:org', 'read:discussion']","combinedSlug needs [...]","name needs [...]","avatarUrl needs [...]"]}
getPullRequestActivity OK: {"comments":2,"commits":4}
listReviewThreadComments FAILED: {"_tag":"GitHubApiResponseError","status":200,"refusedFields":["login needs ['read:org']" x4,"combinedSlug needs [...]","name needs [...]","avatarUrl needs [...]"]}

#35910 and #38514 failed the same way.

After, on this branch:

target: pingdotgg/t3code#40694 on github.com
getPullRequestDetail OK: {"number":40694,"state":"open","reviewDecision":null,"checksState":"passing","checks":45,...}
getPullRequestActivity OK: {"comments":2,"commits":4}
listReviewThreadComments OK: {..., "reviewers":1}
target: pingdotgg/t3code#35910 on github.com
getPullRequestDetail OK: {"number":35910,"state":"open","reviewDecision":"changes-requested","checksState":"passing","checks":39,...}
listReviewThreadComments OK: {..., "reviewers":3}
target: pingdotgg/t3code#38514 on github.com
getPullRequestDetail OK: {"number":38514,"state":"open","reviewDecision":"approved","checksState":"failing","checks":37,...}
listReviewThreadComments OK: {..., "reviewers":1}

Tests

  • New case in GitHubPullRequestApi.test.ts, "reads PR truth when the credential cannot select Team fields". It fails on main with GitHubApiResponseError: ... The 'slug' field requires one of the following scopes: ['read:org', 'read:discussion']. It passes here. It checks the failing check, the review decision, the reviewer login, the Team marker, that the fallback is cached (no second refusal), teamDetails: false on the review-thread read, and that a later reviewer refusal still fails with the exact GitHub error after both retries.
  • vp test run GitHubPullRequestApi.test.ts gitHubPullRequestJson.test.ts: 193 passed.
  • vp run check:changed: format, lint, knip (both passes), graphqlgen --check and typecheck of the 11 affected packages pass. Its tests beside the change also pass: 195 passed.

Not checked: a live PR that requests a team. I had no public PR with a pending team request to read. The unit test covers that case with GitHub's real response shape.

Written by Claude Opus 5.5 in Claude Code via T3 Code.

🤖 Generated with Claude Code

A token with only repo/gist/workflow could not read pull request detail
or review threads: GitHub refuses any query that selects Team fields or
an Organization reactor's login without read:org, even when no team is
requested. Team details are now behind an @include flag. A read that
GitHub answers only with INSUFFICIENT_SCOPES is retried once without
them, and the fallback is remembered per credential and host for ten
minutes. The Reactions fragment no longer selects Organization login.

Supersedes pingdotgg#32366.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 22, 2026 08:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions github-actions Bot added vouch:unvouched Author is not yet trusted in the VOUCHED list. size:L 100-499 changed lines (additions + deletions). labels Nov 22, 2026
@coderabbitai

coderabbitai Bot commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The GitHub pull-request API now retries reads without Team reviewer details after insufficient-scope errors and caches the refusal. Reaction schemas and queries no longer model Organization reactors or request their logins.

Changes: Team detail scope fallback

Layer / File(s) Summary
Conditional Team reviewer fields
packages/source-control-github/src/server/pullRequestReads.graphql, packages/source-control-github/src/server/pullRequestReads.graphql.ts, packages/source-control-github/src/server/pullRequestReviews.graphql, packages/source-control-github/src/server/pullRequestReviews.graphql.ts
Pull-request and review-thread queries accept a teamDetails variable to conditionally request Team fields. Generated schemas reflect optional Team details.
Team request mapping
packages/source-control-github/src/server/gitHubPullRequestJson.ts
Requested reviewer data preserves __typename. Team-request detection recognizes the Team typename and retains the existing fallback checks.
Scope retry and regression coverage
packages/source-control-github/src/server/GitHubPullRequestApi.ts, packages/source-control-github/src/server/GitHubPullRequestApi.test.ts
The API retries insufficient-scope-only responses without Team details and caches the refusal for 10 minutes by credential and host. Tests cover the retry, later reads, and reviewer-field errors.

Organization reactor query changes

Layer / File(s) Summary
Organization reactor schema and selections
packages/source-control-github/src/server/issues.graphql.ts, packages/source-control-github/src/server/pullRequestReviews.graphql, packages/source-control-github/src/server/pullRequestReviews.graphql.ts
Generated reactor schemas classify Organization as an other typename. Issue, review-thread, and review-comment reaction fragments no longer select Organization logins.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant API as GitHubPullRequestApi
  participant GraphQL as GitHub GraphQL API
  participant Cache as Credential and host refusal cache
  API->>GraphQL: Request reviewer details with teamDetails true
  GraphQL-->>API: Return INSUFFICIENT_SCOPES errors
  API->>Cache: Cache Team detail refusal for 10 minutes
  API->>GraphQL: Retry reviewer read with teamDetails false
Loading

Possibly related PRs

  • pingdotgg/t3code#32366: Describes the same insufficient-scope fallback, including retrying without Team metadata and caching the refusal by credential and host.

Suggested reviewers: juliusmarminge, macroscopeapp

Merge Risk: Minimal · up to 94c8a

The change lets pull-request watches work with tokens that lack organization scopes by retrying without Team reviewer details. The only residual concern is a narrow race in credential selection that could briefly hide Team details, so the merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title follows conventional commit format and names the main change: pull-request watch reads now work without organization scopes. The changeset adds a team-details flag, an INSUFFICIENT_SCOPES re…
Description check Passed The description follows the repository template. It has Problem, Change, Scope, and Verification sections, and each section has specific content. The Problem section explains the scope refusal and the…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title follows conventional commit format and names the main change: pull-request watch reads now work without organization scopes. The changeset adds a team-details flag, an INSUFFICIENT_SCOPES retry, and a reduced Reactions fragment, all aimed at that goal. The title is specific and matches the primary fix.

Full details: Description check

Explanation

The description follows the repository template. It has Problem, Change, Scope, and Verification sections, and each section has specific content. The Problem section explains the scope refusal and the affected fields. The Change section lists the code changes and the cache behavior. The Scope section links the superseded PR #32366 and justifies the small fix. The Verification section gives real-GitHub results, unit test details, and unchecked items. The template's conventional commit title guidance is met by the title. The description does not state a linked issue for the bug, but it explains why the fix needs no issue, as the template allows for small fixes. The agent footer is present, as the template asks.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between cd3a57a and 94c8abe.

Files selected for processing: 8

  • packages/source-control-github/src/server/GitHubPullRequestApi.test.ts
  • packages/source-control-github/src/server/GitHubPullRequestApi.ts
  • packages/source-control-github/src/server/gitHubPullRequestJson.ts
  • packages/source-control-github/src/server/issues.graphql.ts
  • packages/source-control-github/src/server/pullRequestReads.graphql
  • packages/source-control-github/src/server/pullRequestReads.graphql.ts
  • packages/source-control-github/src/server/pullRequestReviews.graphql
  • packages/source-control-github/src/server/pullRequestReviews.graphql.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L 100-499 changed lines (additions + deletions). vouch:unvouched Author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants